package com.team10.hotel.controller;

import com.team10.hotel.domain.ParentPermission;
import com.team10.hotel.service.ParentPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/parentPermission")
public class ParentPermissionController {

    @Autowired
    private ParentPermissionService parentPermissionService;

    @RequestMapping("/getAll")
    public String getAllParentPermission(Model model){
        List<ParentPermission> parentPermissionList = parentPermissionService.findAll();
        model.addAttribute("parentPermissionList",parentPermissionList);
        return "permissionParentList";
    }

    @RequestMapping("/add")
    public String addParentPermission(Model model){
        model.addAttribute("parentPermission",new ParentPermission());
        return "permissionParentAdd";
    }

    @RequestMapping("/edit")
    public String editInfoPrint(String id,Model model){
        ParentPermission parentPermission=parentPermissionService.findByPrimaryKey(Integer.parseInt(id));
        model.addAttribute("parentPermission",parentPermission);
        return "permissionParentAdd";
    }

    @RequestMapping("/save")
    public String saveParentPermission(ParentPermission parentPermission){
        parentPermissionService.saveOrUpdate(parentPermission);
        return "redirect:/parentPermission/getAll";
    }

    @RequestMapping("/delete")
    public String deleteParentPermission(String id){
        int del;
        del=parentPermissionService.deleteParentPermission(Integer.parseInt(id));
        if(del==1) return "redirect:/parentPermission/getAll";
        else return "404";
    }

}
